<template>
  <div class="editor-countdown" :style="{ backgroundColor: props.backgroundColor }">
    <div 
      v-if="props.showTitle" 
      class="countdown-title" 
      :style="{ color: props.titleColor }"
    >
      {{ props.title }}
    </div>
    
    <div class="countdown-timer">
      <div v-if="props.showDays" class="countdown-item">
        <div 
          class="countdown-digit" 
          :style="{ 
            color: props.digitColor, 
            backgroundColor: props.digitBackgroundColor 
          }"
        >
          {{ timeLeft.days }}
        </div>
        <div class="countdown-label">天</div>
      </div>
      
      <div v-if="props.showHours" class="countdown-item">
        <div 
          class="countdown-digit" 
          :style="{ 
            color: props.digitColor, 
            backgroundColor: props.digitBackgroundColor 
          }"
        >
          {{ timeLeft.hours }}
        </div>
        <div class="countdown-label">时</div>
      </div>
      
      <div v-if="props.showMinutes" class="countdown-item">
        <div 
          class="countdown-digit" 
          :style="{ 
            color: props.digitColor, 
            backgroundColor: props.digitBackgroundColor 
          }"
        >
          {{ timeLeft.minutes }}
        </div>
        <div class="countdown-label">分</div>
      </div>
      
      <div v-if="props.showSeconds" class="countdown-item">
        <div 
          class="countdown-digit" 
          :style="{ 
            color: props.digitColor, 
            backgroundColor: props.digitBackgroundColor 
          }"
        >
          {{ timeLeft.seconds }}
        </div>
        <div class="countdown-label">秒</div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineProps, reactive, onMounted, onBeforeUnmount } from 'vue';

const props = defineProps({
  endTime: {
    type: String,
    default: '2023-12-31 23:59:59'
  },
  title: {
    type: String,
    default: '活动倒计时'
  },
  showTitle: {
    type: Boolean,
    default: true
  },
  showDays: {
    type: Boolean,
    default: true
  },
  showHours: {
    type: Boolean,
    default: true
  },
  showMinutes: {
    type: Boolean,
    default: true
  },
  showSeconds: {
    type: Boolean,
    default: true
  },
  backgroundColor: {
    type: String,
    default: '#ffffff'
  },
  titleColor: {
    type: String,
    default: '#333333'
  },
  digitColor: {
    type: String,
    default: '#8B4513'
  },
  digitBackgroundColor: {
    type: String,
    default: '#f5f5f5'
  }
});

const timeLeft = reactive({
  days: '00',
  hours: '00',
  minutes: '00',
  seconds: '00'
});

// 更新倒计时
const updateCountdown = () => {
  const now = new Date().getTime();
  const endDate = new Date(props.endTime).getTime();
  const timeRemaining = endDate - now;
  
  if (timeRemaining > 0) {
    const days = Math.floor(timeRemaining / (1000 * 60 * 60 * 24));
    const hours = Math.floor((timeRemaining % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    const minutes = Math.floor((timeRemaining % (1000 * 60 * 60)) / (1000 * 60));
    const seconds = Math.floor((timeRemaining % (1000 * 60)) / 1000);
    
    timeLeft.days = days.toString().padStart(2, '0');
    timeLeft.hours = hours.toString().padStart(2, '0');
    timeLeft.minutes = minutes.toString().padStart(2, '0');
    timeLeft.seconds = seconds.toString().padStart(2, '0');
  } else {
    timeLeft.days = '00';
    timeLeft.hours = '00';
    timeLeft.minutes = '00';
    timeLeft.seconds = '00';
  }
};

let countdownInterval = null;

onMounted(() => {
  updateCountdown();
  countdownInterval = setInterval(updateCountdown, 1000);
});

onBeforeUnmount(() => {
  if (countdownInterval) {
    clearInterval(countdownInterval);
  }
});

defineOptions({
  name: 'editor-countdown'
});
</script>

<style lang="scss" scoped>
.editor-countdown {
  width: 100%;
  padding: 15px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  
  .countdown-title {
    text-align: center;
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 15px;
  }
  
  .countdown-timer {
    display: flex;
    justify-content: center;
    gap: 10px;
    
    .countdown-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      
      .countdown-digit {
        min-width: 40px;
        height: 40px;
        border-radius: 6px;
        font-size: 20px;
        font-weight: 700;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 5px;
      }
      
      .countdown-label {
        font-size: 12px;
        color: #666;
      }
    }
  }
  
  &:hover {
    outline: 1px dashed #ccc;
  }
}
</style> 